<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <!-- react容器 -->
    <div id="test"></div>

    <!-- 引入react核心库 -->
    <script src="../lib/react.js"></script>
    <!-- 引入react-dom -->
    <script src="../lib/react-dom.js"></script>
    <!-- 引入babel -->
    <script src="../lib/babel.min.js"></script>
    <script type="text/babel">
      //1 创建组件

      class Login extends React.Component {
        state = {
          userName: "", //用户名
          passWord: "", //密码
        };
        handleSubmit = (event) => {
          const { userName, passWord } = this.state;
          event.preventDefault();
          alert(userName, passWord);
        };
        // saveUserName = (event) => {
        //   // console.log(event.target.value)
        //   this.setState({ userName: event.target.value });
        // };
        // savePassWord = (event) => {
        //   // console.log(event.target.value)
        //   this.setState({ passWord: event.target.value });
        // };
        saveFormData = (data) => {
          return (event) => {
            console.log(event.target.value, data);
            this.setState({
              [data]: event.target.value,
            });
          };
        };
        render() {
          return (
            <form onSubmit={this.handleSubmit}>
              用户名：
              <input
                onChange={this.saveFormData("userName")}
                type="text"
                name="username"
              />
              密码：
              <input
                onChange={this.saveFormData("passWord")}
                type="text"
                name="password"
              />
              <button>登录</button>
            </form>
          );
        }
      }
      //2 渲染虚拟DOM到页面
      ReactDOM.render(<Login />, document.getElementById("test"));
    </script>
  </body>
</html>
